<?php

namespace application\Components;

use application\Components\CException;
use application\Comm\Request;

class BaseController
{

    private $extention = '.php';
    protected $defauleLayout = 'main';
    public $route;
    public $title = '皮阿诺定制-全屋家具定制专家,专业的定做家具领导品牌';
    public $keyword = '皮阿诺定制,全屋家具,定制家具,定做家具,家具品牌';
    public $description = '皮阿诺定制,定做家具领导品牌,专业为你家客餐厅、厨房、卧室、书房、儿童房，提供具有功能性、收纳性、装饰性的定制家具服务,5大免费服务项目,一站式家具网购就在皮阿诺定制商城。';

    public function __construct()
    {
        $this->beforeAction();
    }

    protected function authorities($key)
    {
        return false;
    }

    /**
     * 返回解析后视图文件内容
     */
    public function renderFile($_viewFile_, $_data_ = null)
    {
        $filename = VIEWS_PATH . '/' . $_viewFile_ . $this->extention;
        if (is_array($_data_)) {
            extract($_data_, EXTR_PREFIX_SAME, 'data');
        }
        ob_start();
        ob_implicit_flush(false);
        include_once $filename;
        return ob_get_clean();
    }

    /**
     * @param $contents
     * @throws Exception
     * 渲染布局文件
     */
    public function renderLayout($contents = '')
    {
        $layoutFile = VIEWS_PATH . '/layouts/' . $this->defauleLayout . $this->extention;
        if (is_file($layoutFile)) {
            require $layoutFile;
        } else {
            throw new CException("{$this->defauleLayout} cannot find the requested layout", 404);
        }
    }

    public function render($_viewFile_, $_data_ = null, $_return_ = false)
    {
        $controller = get_class($this);
        $filename = VIEWS_PATH . '/' . $_viewFile_ . $this->extention;
        $content = $this->renderFile($_viewFile_, $_data_);
        $this->renderLayout($content);
        if (file_exists($filename)) {
            if (is_array($_data_)) {
                extract($_data_, EXTR_PREFIX_SAME, 'data');
            }
            if ($_return_) {
                return file_get_contents($filename);
            } else {
                include_once($filename);
            }
        } else {
            throw new CException("{$controller} cannot find the requested view {$_viewFile_}", 404);
        }
    }

    public function renderPartial($_viewFile_, $_data_ = null, $_return_ = false)
    {
        $controller = get_class($this);
        $filename = VIEWS_PATH . '/' . $_viewFile_ . $this->extention;
        if (file_exists($filename)) {
            if (is_array($_data_)) {
                extract($_data_, EXTR_PREFIX_SAME, 'data');
            }
            if ($_return_) {
                return file_get_contents($filename);
            } else {
                include_once($filename);
            }
        } else {
            throw new CException("{$controller} cannot find the requested view {$_viewFile_}", 404);
        }
    }

    public function redirect($url, $statusCode = 302)
    {
        if (is_array($url)) {
            $route = isset($url[0]) ? $url[0] : '';
            $url = \application\Comm\Func::createUrl($route, array_splice($url, 1));
        }
        header('Location: ' . $url, true, $statusCode);
    }

    public function __call($name, $arguments)
    {
        throw new CException(get_class($this) . " cannot find the requested method {$name}", 404);
    }

    public function __destruct()
    {
        $this->afterAction();
    }

    protected function beforeAction()
    {
        $this->route = isset($_GET['r']) ? $_GET['r'] : 'site/index';
        return true;
        $public_action = [
            'site/captcha',
            'site/logout',
        ];

        if (!in_array($this->route, $public_action)) {
            if (!isset($_SESSION['admin'])) {
                $this->redirect(['site/login']);
            } else {
                return true;
            }
        } else {
            return true;
        }


    }

    /**
     * 判断是否显示该功能
     * @param string $key
     * @return bool
     */
    public function checkAuthority($key = '')
    {
        $roleArr = array(1); //全部可视角色
        $actionId = $this->authorities($key);

        if ($actionId === true) {
            return true;
        }
        $login = isset($_SESSION['admin']);
        if (!$login) {
            return false;
        }
        $sessionUser = $_SESSION['admin'];
        $userAuthority = $sessionUser['authority'];
        $userRole = $sessionUser['role_id'];

        if (in_array($userRole, $roleArr)) {
            return true;
        }
        if (in_array($actionId, $userAuthority)) {
            return true;
        }

    }

    protected function afterAction()
    {
        return true;
    }

}
